<template>
  <div class="flex items-center justify-center min-h-screen">
    <div class="text-center">
      <div class="loading loading-spinner loading-lg"></div>
    </div>
  </div>
</template>
<script setup>
definePageMeta({
  layout: "no-sidebar",
});
import { useRoute } from "vue-router";
import { getHttp } from "~/composables/common/http";
import { userSet } from "@/composables/common/user";
const route = useRoute();
const http = getHttp();
const handleLogin = async () => {
  if (!route.query.code) {
    return navigateTo("/");
  }
  const tokenData = await http("/authing/getToken", {
    method: "post",
    body: {
      code: route.query.code,
    },
  });
  if (!tokenData?.access_token) {
    return navigateTo("/");
  }
  const userData = await http("/user/getUser", {
    method: "post",
    body: {
      access_token: tokenData.access_token,
    },
  });
  if (userData?.mobile) {
    userSet().saveUserData(
      userData.name,
      userData.photo,
      userData.mobile,
      tokenData.access_token
    );
  }
  return navigateTo("/");
};
onMounted(() => {
  handleLogin();
});
</script>
